nokia.Settings.set("appId","qC7DnY3LjuPXsBb2gPXx");
nokia.Settings.set("authenticationToken","GaCfM4N_3OyOTBJwNjHBJw");

var $direccion_parada= $('#ParadaDireccionParada');
var $latitud= $('#ParadaLatitud');
var $longitud= $('#ParadaLongitud');
var	resultSet = new nokia.maps.map.Container();
var enterByDirec = false;
var searchManager = nokia.places.search.manager;
var coord;

var mapCl = document.getElementById("map");
var map =  new nokia.maps.map.Display(mapCl, {
	center: [6.238226,-75.585092],
	zoomLevel : 11,
	components:[
		new nokia.maps.map.component.ZoomBar(), 
		new nokia.maps.map.component.Behavior(),
		new nokia.maps.map.component.TypeSelector(),
		new nokia.maps.map.component.ScaleBar()
		]
});


var listener = function (evt) {
	/* We check if this drag listener is called while the marker is being
	 * dragged using the default behavior component
	 */
	var mapDragType = evt.dataTransfer.getData("application/map-drag-type"); 

	if (mapDragType === "marker" || mapDragType === "standardmarker") {
		var marker = evt.dataTransfer.getData("application/map-drag-object");
		var offset = evt.dataTransfer.getData("application/map-drag-object-offset");
		var coordinate = map.pixelToGeo(evt.displayX - offset.x + marker.anchor.x, evt.displayY - offset.y + marker.anchor.y);
		
		if (evt.dataTransfer.dropEffect === "none") {
			if (evt.type === "dragend"){
				// searchManager.reverseGeoCode({
				// 	latitude: coordinate.latitude,
				// 	longitude: coordinate.longitude,
				// 	onComplete: processResults
				// });
				processResults2(coordinate);
			}
			else {

			}
		}
		else {
				/*searchManager.reverseGeoCode({
					latitude: coord.latitude,
					longitude: coord.longitude,
					onComplete: processResults
				});*/
		}
	}
};

var processResults2 = function (coordinate) {
	if (resultSet) map.objects.remove(resultSet);
	resultSet = new nokia.maps.map.Container();
	marker = new nokia.maps.map.StandardMarker(coordinate, { text: 1, draggable: true });
	marker.addListener("drag", listener, true);
	marker.addListener("dragend", listener, false);
	resultSet.objects.add(marker);
	map.objects.add(resultSet);
	$latitud.val(coordinate.latitude);
	$longitud.val(coordinate.longitude);
}

var processResults = function (data, requestStatus, requestId) {
	var i, len, locations, marker;
	
	if (requestStatus == "OK") {
		locations = data.results ? data.results.items : [data.location];
		if (locations.length > 0) {
			if (resultSet) map.objects.remove(resultSet);
			resultSet = new nokia.maps.map.Container();
			for (i = 0, len = locations.length; i < len; i++) {
				marker = new nokia.maps.map.StandardMarker(locations[i].position, { text: i+1, draggable: true });
				marker.addListener("drag", listener, true);
				marker.addListener("dragend", listener, false);
				resultSet.objects.add(marker);
				$latitud.val(locations[i].position.latitude);
				$longitud.val(locations[i].position.longitude);
				//$direccion_parada.val(locations[i].address.text);
			}
				map.objects.add(resultSet);
				if(enterByDirec)
				map.zoomTo(resultSet.getBoundingBox(), false);
			enterByDirec=false;
		} else { 
			alert("Your search produced no results!");
		}
	} else {
		alert("The search request failed");
	}
};

$(document).ready(function(){
	
	var TOUCH = nokia.maps.dom.Page.browser.touch,
	CLICK = TOUCH ? "tap" : "click",
	addedCoords = [];
	if (window.app.editar==true){
		coordenadas = new nokia.maps.geo.Coordinate(parseFloat($latitud.val()),parseFloat($longitud.val()));
		var marker2 = new nokia.maps.map.StandardMarker(coordenadas, { text: 1 , draggable: true });
		marker2.addListener("drag", listener, true);
		marker2.addListener("dragend", listener, false);
		resultSet.objects.add(marker2);
		map.objects.add(resultSet);
		map.zoomTo(resultSet.getBoundingBox(), false);
	}
	$direccion_parada.change(function(){
    	if($direccion_parada.val()!="" && $direccion_parada.val()!=null){
    		enterByDirec=true;
			searchManager.geoCode({searchTerm: $direccion_parada.val(),onComplete: processResults});
		}
	});

	/*var contextMenu = new nokia.maps.map.component.ContextMenu(),
	customHandler = function(contextMenuEvent, group) {
		var context = contextMenuEvent.target;
		if (context instanceof nokia.maps.map.Display) {
			group.addEntry("Seleccionar esta dirección", function() {
				searchManager.reverseGeoCode({
					latitude: coord.latitude,
					longitude: coord.longitude,
					onComplete: processResults
				});
			});
			group.addEntry(coord.toString());
		}
	};
	contextMenu.addHandler(customHandler);
	map.components.add(contextMenu);*/
	// map.addListener(CLICK, function (evt) {
	// 	coord = map.pixelToGeo(evt.displayX, evt.displayY);
	// 	searchManager.reverseGeoCode({
	// 				latitude: coord.latitude,
	// 				longitude: coord.longitude,
	// 				onComplete: processResults
	// 			});
	// });
});




